package main.java.com.zyh.entity;

import main.java.com.zyh.util.SQLiteExample;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class LoginWindow {
    private static User user = null;
    private static final int WIDTH = 300;
    private static final int HEIGHT = 200;
    static final JFrame loginFrame = new JFrame("登录界面");

    public LoginWindow() {

        loginFrame.setSize(WIDTH, HEIGHT);
        loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel loginPanel = new JPanel();
        loginFrame.add(loginPanel);

        //loginFrame.setContentPane(loginPanel);
        placeComponents(loginPanel);
        loginFrame.setVisible(true);
    }
    private static void placeComponents(JPanel panel) {

        /* 布局部分我们这边不多做介绍
         * 这边设置布局为 null
         */
        panel.setLayout(null);

        // 创建 JLabel
        JLabel userLabel = new JLabel("User:");
        /* 这个方法定义了组件的位置。
         * setBounds(x, y, width, height)
         * x 和 y 指定左上角的新位置，由 width 和 height 指定新的大小。
         */
        userLabel.setBounds(10,20,80,25);
        panel.add(userLabel);

        /*
         * 创建文本域用于用户输入
         */
        JTextField userText = new JTextField(20);
        userText.setBounds(100,20,165,25);
        panel.add(userText);

        // 输入密码的文本域
        JLabel passwordLabel = new JLabel("Password:");
        passwordLabel.setBounds(10,50,80,25);
        panel.add(passwordLabel);

        /*
         *这个类似用于输入的文本域
         * 但是输入的信息会以点号代替，用于包含密码的安全性
         */
        JPasswordField passwordText = new JPasswordField(20);
        passwordText.setBounds(100,50,165,25);
        panel.add(passwordText);

        // 创建登录按钮
        JButton loginButton = new JButton("login");
        loginButton.addActionListener(new ActionListener() {

            /**
             * Invoked when an action occurs.
             *
             * @param e the event to be processed
             */
            @Override
            public void actionPerformed(ActionEvent e) {
                String userName = userText.getText();
                String userPassword = passwordText.getText();
                System.out.println(userName+" "+userPassword);
                if(userName.equals("admin") && userPassword.equals("123456")){
                    new A(user);
                    loginFrame.dispose();
                }else {
                    JOptionPane.showMessageDialog(null, "用户名或密码错误！", "消息提示", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });
        loginButton.setBounds(10, 80, 80, 25);
        panel.add(loginButton);
    }
}
class A{
    private static final int WIDTH = 800;
    private static final int HEIGHT = 600;

    public A(User user) {
        final JFrame oneFrame = new JFrame("管理系统");
        oneFrame.setSize(WIDTH, HEIGHT);
        oneFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel onePanel = new JPanel();
        oneFrame.add(onePanel);

        //loginFrame.setContentPane(loginPanel);
        placeComponents(onePanel);
        oneFrame.setVisible(true);
    }
    private static void placeComponents(JPanel panel) {
        panel.setLayout(null);
    }
}